<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>自定义指令实践 | Jaho’s Blog</title>
    <meta name="generator" content="VuePress 1.8.0">
    <link rel="icon" href="/favicon.ico">
    <link rel="manifest" href="/manifest.json">
    <meta name="description" content="记录日常学习工作">
    
    <link rel="preload" href="/assets/css/0.styles.5b8fe0a4.css" as="style"><link rel="preload" href="/assets/js/app.6514719e.js" as="script"><link rel="preload" href="/assets/js/2.ea114ffb.js" as="script"><link rel="preload" href="/assets/js/41.ced85449.js" as="script"><link rel="preload" href="/assets/js/4.e093c564.js" as="script"><link rel="prefetch" href="/assets/js/10.13203aa5.js"><link rel="prefetch" href="/assets/js/11.099868c4.js"><link rel="prefetch" href="/assets/js/12.c8365e17.js"><link rel="prefetch" href="/assets/js/13.89751e7c.js"><link rel="prefetch" href="/assets/js/14.31f563eb.js"><link rel="prefetch" href="/assets/js/15.08b381b6.js"><link rel="prefetch" href="/assets/js/16.1a3867de.js"><link rel="prefetch" href="/assets/js/17.7dd4e0c6.js"><link rel="prefetch" href="/assets/js/18.907c4597.js"><link rel="prefetch" href="/assets/js/19.fac8aa1a.js"><link rel="prefetch" href="/assets/js/20.73fe0e75.js"><link rel="prefetch" href="/assets/js/21.5c50aa1c.js"><link rel="prefetch" href="/assets/js/22.83d03663.js"><link rel="prefetch" href="/assets/js/23.061e7ac0.js"><link rel="prefetch" href="/assets/js/24.cc7e9011.js"><link rel="prefetch" href="/assets/js/25.fa6c7aef.js"><link rel="prefetch" href="/assets/js/26.39392f70.js"><link rel="prefetch" href="/assets/js/27.204436be.js"><link rel="prefetch" href="/assets/js/28.3f54c157.js"><link rel="prefetch" href="/assets/js/29.799d048c.js"><link rel="prefetch" href="/assets/js/3.6c5d6214.js"><link rel="prefetch" href="/assets/js/30.deed02f3.js"><link rel="prefetch" href="/assets/js/31.5916873b.js"><link rel="prefetch" href="/assets/js/32.b0634144.js"><link rel="prefetch" href="/assets/js/33.734675b8.js"><link rel="prefetch" href="/assets/js/34.20404dc4.js"><link rel="prefetch" href="/assets/js/35.d93b829f.js"><link rel="prefetch" href="/assets/js/36.b0692d8d.js"><link rel="prefetch" href="/assets/js/37.3dffa306.js"><link rel="prefetch" href="/assets/js/38.04649a97.js"><link rel="prefetch" href="/assets/js/39.7dfcdc32.js"><link rel="prefetch" href="/assets/js/40.04914511.js"><link rel="prefetch" href="/assets/js/42.72739213.js"><link rel="prefetch" href="/assets/js/43.de1cff16.js"><link rel="prefetch" href="/assets/js/44.48e301ae.js"><link rel="prefetch" href="/assets/js/45.870a7650.js"><link rel="prefetch" href="/assets/js/46.657f15b8.js"><link rel="prefetch" href="/assets/js/47.3df1bfdb.js"><link rel="prefetch" href="/assets/js/48.f4e4e774.js"><link rel="prefetch" href="/assets/js/49.632bf611.js"><link rel="prefetch" href="/assets/js/5.6531e181.js"><link rel="prefetch" href="/assets/js/50.a8018493.js"><link rel="prefetch" href="/assets/js/51.cb424f59.js"><link rel="prefetch" href="/assets/js/52.a9f595dc.js"><link rel="prefetch" href="/assets/js/53.cf8a9ec0.js"><link rel="prefetch" href="/assets/js/54.46d774a3.js"><link rel="prefetch" href="/assets/js/55.3574f9b8.js"><link rel="prefetch" href="/assets/js/56.baf60ba5.js"><link rel="prefetch" href="/assets/js/57.af42b559.js"><link rel="prefetch" href="/assets/js/58.9300cb59.js"><link rel="prefetch" href="/assets/js/6.00f437ec.js"><link rel="prefetch" href="/assets/js/7.58ac7166.js"><link rel="prefetch" href="/assets/js/8.1b9c5002.js"><link rel="prefetch" href="/assets/js/9.5c076362.js">
    <link rel="stylesheet" href="/assets/css/0.styles.5b8fe0a4.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="Jaho’s Blog" class="logo"> <span class="site-name can-hide">Jaho’s Blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/posts/" class="nav-link router-link-active">
  技术博客
</a></div> <a href="https://juejin.cn/user/26836627369806" target="_blank" rel="noopener noreferrer" class="repo-link">
    个人主页
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/posts/" class="nav-link router-link-active">
  技术博客
</a></div> <a href="https://juejin.cn/user/26836627369806" target="_blank" rel="noopener noreferrer" class="repo-link">
    个人主页
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><a href="/posts/" aria-current="page" class="sidebar-link">写在前面</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>工作问题解决</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>JavaScript</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Css</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Nodejs</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Vue</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/posts/vue/自定义指令实践.html" class="active sidebar-link">自定义指令实践</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/posts/vue/自定义指令实践.html#vue指令" class="sidebar-link">vue指令</a></li><li class="sidebar-sub-header"><a href="/posts/vue/自定义指令实践.html#局部挂载" class="sidebar-link">局部挂载</a></li><li class="sidebar-sub-header"><a href="/posts/vue/自定义指令实践.html#具体效果" class="sidebar-link">具体效果</a></li><li class="sidebar-sub-header"><a href="/posts/vue/自定义指令实践.html#全局挂载" class="sidebar-link">全局挂载</a></li></ul></li><li><a href="/posts/vue/内容分发.html" class="sidebar-link">内容分发（插槽）</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Git</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="自定义指令实践"><a href="#自定义指令实践" class="header-anchor">#</a> 自定义指令实践</h1> <blockquote><p>总结一些日常需要用到的一些api，也是在一些面试中会经常出现的题目，今天分享的是vue3中使用自定义指令封装拖拽方法， 同时文章也被收录到我的《JS基础》专栏中，欢迎大家点击收藏加关注。</p></blockquote> <h2 id="vue指令"><a href="#vue指令" class="header-anchor">#</a> vue指令</h2> <blockquote><p>vue中有内置的一些指令供我们使用，<code>v-model</code> 或 <code>v-show</code>是比较常见的指令，绑定在对应的元素上，可以对元素的样式或者值进行一些对应的JS操作，除了vue内置的一些指令之外，如果业务需要，我们也会书写一些自己的自定义指令，自定义指令主要是为了重用涉及普通元素的底层 DOM 访问的逻辑，也就是封装一些对dom的操作</p></blockquote> <p>今天在项目中使用自定义指令写一个拖拽操作，主要记录一下实现的过程，局部挂载和全局挂载的区别，由于生命周期大部份情况下都可能用不到，所以我们只记录下mouted生命周期的绑定。</p> <h2 id="局部挂载"><a href="#局部挂载" class="header-anchor">#</a> 局部挂载</h2> <p>在 <code>&lt;script setup&gt;</code> 中，任何以 <code>v</code> 开头的驼峰式命名的变量都可以被用作一个自定义指令。在下面的例子中，<code>vDrag</code> 即可以在模板中以 <code>v-drag</code> 的形式使用</p> <p>这里大概说下指令内的内容，声明一个名为vDrag的指令，犹豫vDrag是绑定在元素上的，所以mounted表示元素在<code>绑定元素的父组件及他自己的所有子节点都挂载完成后调用</code>，调用的时候传入el，el是绑定的dom元素</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// layout.vue</span>
<span class="token operator">&lt;</span>script setup lang<span class="token operator">=</span><span class="token string">&quot;ts&quot;</span><span class="token operator">&gt;</span>
<span class="token keyword">const</span> vDrag <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token function-variable function">mounted</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token literal-property property">el</span> <span class="token operator">:</span>HTMLElement</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>el<span class="token punctuation">)</span>
    el<span class="token punctuation">.</span>style<span class="token punctuation">.</span>cursor <span class="token operator">=</span> <span class="token string">'grab'</span><span class="token punctuation">;</span>
    el<span class="token punctuation">.</span><span class="token function-variable function">onmousedown</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token parameter">e</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>  
      document<span class="token punctuation">.</span><span class="token function-variable function">onmousemove</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">e</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        e<span class="token punctuation">.</span><span class="token function">preventDefault</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 移动时禁用默认事件 否则拖动的时候会有将元素拖动出来的效果</span>

        <span class="token comment">// 计算偏移距离 鼠标x - 元素x</span>
        <span class="token keyword">let</span> left <span class="token operator">=</span> e<span class="token punctuation">.</span>clientX <span class="token operator">-</span> el<span class="token punctuation">.</span>offsetLeft
        <span class="token keyword">let</span> <span class="token literal-property property">sider</span><span class="token operator">:</span>HTMLElement <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.n-layout-sider'</span><span class="token punctuation">)</span><span class="token operator">!</span> <span class="token comment">//不能将类型“HTMLElement | null”分配给类型“HTMLElement” 这里可以使用断言</span>
        
        sider<span class="token punctuation">.</span>style<span class="token punctuation">.</span>width <span class="token operator">=</span> el<span class="token punctuation">.</span>offsetLeft <span class="token operator">+</span> left <span class="token operator">+</span> <span class="token string">'px'</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>el<span class="token punctuation">.</span>offsetLeft <span class="token operator">+</span> left <span class="token punctuation">)</span>
        <span class="token comment">// console.log('clientX',e.clientX)</span>
        <span class="token comment">// console.log('offsetLeft',el.offsetLeft) //元素和屏幕的左边距</span>
      <span class="token punctuation">}</span>
      document<span class="token punctuation">.</span><span class="token function-variable function">onmouseup</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//按压时间结束的时候 要禁用掉down的监听事件 否则会一直执行</span>
          document<span class="token punctuation">.</span>onmousemove <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
          document<span class="token punctuation">.</span>onmouseup <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</span>

<span class="token operator">&lt;</span>template<span class="token operator">&gt;</span>
    <span class="token operator">...</span>
        <span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;line&quot;</span> v<span class="token operator">-</span>drag <span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">&gt;</span>
    <span class="token operator">...</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>template<span class="token operator">&gt;</span>
</code></pre></div><p>接下来是一系列的实现拖拽的代码，mounted的时候改变鼠标悬浮效果，接着按下鼠标禁用默认事件，监听鼠标x轴上的移动距离，减掉元素宽度，由于要实现宽度拖拽，所以要改变的其实是<code>n-layout-sider</code>的宽度，这部分还不够完善，大家仅供参考，最后是鼠标抬起事件，将前面的监听事件down和up取消掉，否则会一直监听。</p> <h2 id="具体效果"><a href="#具体效果" class="header-anchor">#</a> 具体效果</h2> <p>这里我对<code>n-layout-sider</code>的宽度在css规定了最小和最大宽度，所以拖动到左边的时候会有限制。</p> <p><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c83092633de74c2293e82f4395e1d1c1~tplv-k3u1fbpfcp-watermark.image?" alt="move.gif"></p> <h2 id="全局挂载"><a href="#全局挂载" class="header-anchor">#</a> 全局挂载</h2> <blockquote><p>全局中进行使用，这里只要把封装好的指令进行引入，再通过directive挂载到app上，需要注意的是，不同于局部挂载的写法，这里应该写的是'move'而不是'v-move'，因为这是通过directive函数来进行挂载指令，所以只需要写上指令的名称</p></blockquote> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//widthMove.ts</span>
<span class="token keyword">export</span> <span class="token keyword">const</span>  vMove <span class="token operator">=</span> <span class="token punctuation">{</span>
 <span class="token function-variable function">mounted</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token literal-property property">el</span> <span class="token operator">:</span>HTMLElement</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
     <span class="token operator">...</span><span class="token punctuation">.</span>
 <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span>  <span class="token punctuation">{</span>vMove<span class="token punctuation">}</span>  <span class="token keyword">from</span> <span class="token string">&quot;@/utils/widthMove.js&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token function">createApp</span><span class="token punctuation">(</span>App<span class="token punctuation">)</span>
app<span class="token punctuation">.</span><span class="token function">directive</span><span class="token punctuation">(</span><span class="token string">'move'</span><span class="token punctuation">,</span>vMove<span class="token punctuation">)</span> <span class="token comment">// </span>
app<span class="token punctuation">.</span><span class="token function">mount</span><span class="token punctuation">(</span><span class="token string">'#app'</span><span class="token punctuation">)</span> 

</code></pre></div></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2023-01-04 10:56:12</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/posts/node/node操作数据库.html" class="prev">
        node操作数据库
      </a></span> <span class="next"><a href="/posts/vue/内容分发.html">
        内容分发（插槽）
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/assets/js/app.6514719e.js" defer></script><script src="/assets/js/2.ea114ffb.js" defer></script><script src="/assets/js/41.ced85449.js" defer></script><script src="/assets/js/4.e093c564.js" defer></script>
  </body>
</html>
